<?php
	include("initialize.php");
?>
<html>
<head>
	<title>GradeGrid - Account Home</title>
	<?php include("header.php"); ?>
</head>

<body onLoad="writeCopyright()" >
	<div class="main">
	
		<?php include("boilerplate.php"); ?>
		
		<?php if($_SESSION["logged_in"]): ?>
			<?php include("loginnavigation.php"); ?>
			
			<div style="float: left; max-width: 70%;">
			
			<?php
				include("write_functions.php");
				connect("localhost", "root", "", "");
				
				$accountid_query = mysql_query("SELECT account_id FROM account WHERE username='" . $_SESSION["username"] . "'");
				$accountid_array = mysql_fetch_array($accountid_query);
				$accountid = $accountid_array["account_id"];
				
				// Get all start times and ending times separately
				$time_resource = mysql_query("SELECT DISTINCT start_time, end_time FROM schedule"); 
				$all_times = array();
				$i = 0;
				while ($times = mysql_fetch_array($time_resource)) {
					$all_times[$i++] = $times[0];		// Start time
					$all_times[$i++] = $times[1];		// End time
				}
				$all_times = array_unique($all_times);
				sort($all_times);
				
				// Get all classes - assume that data only exists for this semester
				$query = "SELECT DISTINCT class_name, day, start_time, end_time, course_name, year ";
				$query = $query . "FROM class, course, schedule ";
				$query = $query . "WHERE class.class_id = schedule.class_id AND class.course_id = course.course_id AND class.account_id = '$accountid'";
				$query = $query . "ORDER BY start_time, end_time, day";
				$class_resource = mysql_query($query); 
				$the_classes[] = array();
				$i = 0;
				
				while ($classes = mysql_fetch_array($class_resource, MYSQL_ASSOC)) {
					$the_classes[$i++] = $classes;
				}
			?>
			
			<span style="background-color: #DADADA; padding: 5px; font-family: Arial; font-size: 13px; width: 10%; text-align: center;">Grid View</span>
			<a href="class_list.php" style="padding: 5px; width: 10%; text-align: center;">List View</a>
			<div class="form_section"><h1>Class Schedule</h1></div>
			<p>Click to view class details.</p>
			
			<?php
				$day_abbr = array('SN', 'M', 'T', 'W', 'TH', 'F', 'ST');
				
				echo "<table class='schedule pMirror'>";
				// Topmost row: Column header -> days of the week
				echo "<tr>" . 
					"<td class='Schedule'>Time</td>" . 
					"<td class='Schedule'>Sunday</td>" . 
					"<td class='Schedule'>Monday</td>" . 
					"<td class='Schedule'>Tuesday</td>" . 
					"<td class='Schedule'>Wednesday</td>" . 
					"<td class='Schedule'>Thursday</td>" . 
					"<td class='Schedule'>Friday</td>" . 
					"<td class='Schedule'>Saturday</td>" . 
					"</tr>";
				
				$i = 0;											// i: Counter for TIME SLICES
				while ($i < count($all_times) - 1) { 
					$k = 0; 									// k: Counter for DAY OF WEEK, range (0, 6);
					$latest_start_time = 0;						// To reduce loops...
					echo "<tr>";
					echo "<td class='schedule'>";
					echo $all_times[$i] . " - " . $all_times[$i+1];
					echo "</td>";
					
					// Loop through seven days
					while ($k < 7) { 
						echo "<td class='schedule'>";			// Add placeholder
						
						$j = $latest_start_time;				// j: Counter for CLASSES
						while ($j < count($the_classes)) {
							// Test if current class is held on day k
							if ($the_classes[$j][day] == $day_abbr[$k]) {
								// Test if current class fits in current schedule slot
								if ($the_classes[$j][start_time] <= $all_times[$i] &&
									$the_classes[$j][end_time] >= $all_times[$i+1]) {
									
									// Then also, increment counter for CLASSES to check next class :D	
									echo "<form action='record_operations.php' method='get'>";
									echo "<input type='hidden' name='class_name' value='" . $the_classes[$j][course_name]. " '/>";
									echo "<input type='hidden' name='section' value='" . $the_classes[$j][class_name]. " '/>";
									echo "<input type='submit' value='" . $the_classes[$j][course_name]. "' />";
									echo "</form>";
									
									if ($the_classes[$j+1][start_time] >= $all_times[$i+1]) {
										$start_time = $j + 1;
									}
								} 
							}
							$j++;
						}
						$k++;									// Increment DAY OF WEEK counter
						echo "</td>";
					}
					echo "</tr>";								// End ROW
					$i++;										// Increment counter for TIME SLICE
				} 
				echo "</table>";								// End TABLE
			?>
			
			<p> <input type="button" value="Add classes" onClick="window.open('new_class.php', '', 'width=500, height=500')"/> </p>
			
			<!-- Classes and unfulfilled requirements -->
			<div class="form_section"><h1>Class Requirements</h1></div>
			
			<!-- Pwedeng next time na yung Sort By? T_T
			<p>Sort by: <select>
				<option>Class title</option>
				<option>Rubric</option>
			</select></p>
			<!-- For interface purposes only, pwede ring "View incomplete only" or "View all" -->
			
			<?php 
			
			$query = "SELECT class_id, course_name, class_name, template_name, major.name, minor.name FROM course, class, template, major, minor " .
				"WHERE (class.class_id, minor.minor_id) NOT IN " . 
					"(SELECT class.class_id, minor.minor_id  FROM grade, minor, major, class, template " . 
					"WHERE grade.minor_id = minor.minor_id " .
					"AND class.template_id = template.template_id " . 
					"AND minor.major_id = major.major_id " .
					"AND major.template_id = template.template_id " .
					"AND grade.class_id = class.class_id " .
					"AND grade.major_id = major.major_id " .
					"AND grade.minor_id = minor.minor_id " . 
				") " . 
				"AND class.template_id = template.template_id " .
				"AND minor.major_id = major.major_id " . 
				"AND major.template_id = template.template_id " .
				"AND course.course_id = class.course_id  AND class.account_id = '$accountid'" .
				"ORDER BY course_name, class_name";
			$grade_resource = mysql_query($query);
			
			$incomplete_list = array();
			$i = 0;
			while ($grades = mysql_fetch_array($grade_resource, MYSQL_ASSOC)) {
				$incomplete_list[$i++] = $grades;
			}
			
			?>
			
			
			<table class="schedule pMirror" cellpadding="8">
				<tr>
					<td><b><u>Class Title</u></b></td>
					<td><b><u>Rubric</u></b></td>
					<td><b><u>Unfullfilled Requirements</u></b></td>
					<td><b><u>Actions</u></b></td>
				</tr>
				
				<?php
				$i = 0;
				while ($i < count($incomplete_list)) {
					echo "<tr>";								// One row per class with incomplete grades
					
					echo "<td><b>" . $incomplete_list[$i][course_name] . " - " . 
						$incomplete_list[$i][class_name] . "</b></td>";
					
					echo "<td>" . $incomplete_list[$i][template_name] . "</td>";
					
					echo "<td class='schedule' align='center'>";
					do {
						// One box for all reqs of a class
						echo "<p>" . $incomplete_list[$i][name] . "</p>";
						$i++;
					} while ($i < count($incomplete_list) &&
						$incomplete_list[$i-1][class_name] == $incomplete_list[$i][class_name] &&
						$incomplete_list[$i-1][course_name] == $incomplete_list[$i][course_name]);
					echo "</td>";
					$thisclassid = $incomplete_list[$i]["class_id"];
					echo "<td><a href='record_operations.php?classid=$thisclassid'>Complete grades</a></td>";
					echo "</tr>";
				}
				?>
			</table>
			
			</div>	
			
		<?php else: ?>
			<?php executeFile("../files/access_error.txt"); ?>
		<?php endif ?>
		
	</div>
	<div id="copywright"></div>
</body>
</html>